
"""
异常处理装饰器 - 防止界面崩溃
"""

import functools
import traceback
from PySide2.QtWidgets import QMessageBox


def safe_ui_operation(func):
    """安全UI操作装饰器"""
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except Exception as e:
            print(f"UI操作异常: {func.__name__} - {e}")
            traceback.print_exc()
            # 不显示错误对话框，避免进一步崩溃
            return None
    return wrapper


def safe_download_operation(func):
    """安全下载操作装饰器"""
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except Exception as e:
            print(f"下载操作异常: {func.__name__} - {e}")
            # 可以在这里添加日志记录
            raise  # 重新抛出异常，让上层处理
    return wrapper
